package com.itheima.publisher;

import com.itheima.consumer.ConsumerApplication;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.concurrent.ListenableFutureCallback;

@SpringBootTest(classes = {ConsumerApplication.class, PublisherApplication.class})

@Slf4j
public class test01 {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Test
    void test(){
        //publisher-confirm-type: correlated # 开启publisher confirm机制，并设置confirm类型要开启
        CorrelationData cd = new CorrelationData();
        cd.getFuture().addCallback(new ListenableFutureCallback<CorrelationData.Confirm>() {
            //不能发出去
            @Override
            public void onFailure(Throwable ex) {
                log.error("send message failed", ex);
            }
            //能发出去
            @Override
            public void onSuccess(CorrelationData.Confirm result) {
                //只要把消息发送到交换机就会触发ack
                if(result.isAck())
                {
                    log.debug("发送消息成功");
                }
                //没到交换机
                else
                {
                    log.error("发送消息失败,没找到对应的交换机,收到nack,reason:{}", result.getReason());
                }
            }
        });
        rabbitTemplate.convertAndSend("exchange02","zkyy","hello666",cd);
    }
    @Test
    void test1(){
    }

}
